package ck

import (
	"gitee.com/tietang/terrace-go/v2/example/moni/dao"
	"gitee.com/tietang/terrace-go/v2/lib/rpc"
	"github.com/sirupsen/logrus"
	"github.com/tietang/dbx"
)

var ckDubboDao dao.Dao = new(ClickHouseDubboDao)

func init() {
	//dao.Register("dubbo.DubboRequestMetrics", ckDubboDao)
	handler := dao.NewHandler(ckDubboDao)
	rpc.RegisterHandler("dubbo.DubboRequestMetrics", handler)
}

type ClickHouseDubboDao struct {
}

func (d *ClickHouseDubboDao) Save(metrics any) {
	m := metrics.(map[string]interface{})
	nFields := 17

	metricsList := m["metricsList"].([]map[string]interface{})
	args := make([]interface{}, 0, len(metricsList)*nFields)
	for _, met := range metricsList {
		args = append(args, met["appName"])
		args = append(args, met["hostName"])
		args = append(args, met["timestamp"])
		args = append(args, met["interval"])
		args = append(args, met["application"])
		args = append(args, met["service"])
		args = append(args, met["method"])
		args = append(args, met["side"])
		args = append(args, met["clientAddress"])
		args = append(args, met["serverAddress"])
		args = append(args, met["success"])
		args = append(args, met["failure"])
		args = append(args, met["input"])
		args = append(args, met["output"])
		args = append(args, met["elapsed"])
		args = append(args, met["concurrent"])

	}

	Clickhouse().Tx(func(run *dbx.TxRunner) error {
		_, err := run.Exec(dubbo_sql, args...)
		if err != nil {
			logrus.Error(err)
			return err
		}
		return nil
	})

}

func (d *ClickHouseDubboDao) Init() {
	Clickhouse().Tx(func(run *dbx.TxRunner) error {
		_, err := run.Exec(dubbo_createTable)
		if err != nil {
			logrus.Error(err)
			return err
		}
		return nil
	})
}

const dubbo_sql = "INSERT INTO m_dubbo (  " +
	"    appName, hostName, timestamp, interval," +
	"   application, service, method, side, clientAddress, serverAddress, success, failure, input, output, elapsed, concurrent)" +
	" from input (' " +
	"    appName String, hostName String, timestamp Int64, interval Int64," +
	"    application String, service String, method String,  side String, clientAddress String, serverAddress String, success Int64, failure Int64, input Int64, output Int64, elapsed Int64, concurrent Int64" +
	"')"

//?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
//" from input (' " +
//"    appName String, hostName String, timestamp Int64, interval Int64," +
//"    application String, service String, method String,  side String, clientAddress String, serverAddress String, success Int64, failure Int64, input Int64, output Int64, elapsed Int64, concurrent Int64" +
//"')"

const dubbo_createTable = "create table if not exists m_dubbo" +
	" (" +
	" appName String,\n" +
	" hostName String,\n" +
	" timestamp Int64,\n" +
	" interval Int64,\n" +
	" application String,\n" +
	" service String,\n" +
	" method String,\n" +
	" side String,\n" +
	" clientAddress String,\n" +
	" serverAddress String,\n" +
	" success Int64,\n" +
	" failure Int64,\n" +
	" input Int64,\n" +
	" output Int64,\n" +
	" elapsed Int64,\n" +
	" concurrent Int64,\n" +
	" d Date materialized toDate(round(timestamp / 1000))," +
	" dt DateTime materialized toDateTime(round(timestamp / 1000))" +
	" ) " +
	" engine = MergeTree PARTITION BY (toYYYYMM(dt), appName)\n" +
	"        ORDER BY dt\n" +
	"        SETTINGS index_granularity = 8192;"
